var UserNO = localStorage.getItem("UserNO");
mui.plusReady(function() {
	var currentwindow = plus.webview.currentWebview();
	if (currentwindow.BarcodeNO != '' && currentwindow.BarcodeNO != undefined) {
		$("#OldMaterialCode").val(currentwindow.BarcodeNO);
		if (!currentwindow.StockQty) {
			$("#TotalQty").val(currentwindow.Qty);
		} else {
			$("#TotalQty").val(currentwindow.StockQty);
		}
		$("#TradeOrderNO").val(currentwindow.TradeOrderNO);
		$("#NewQty").focus();
	} else {
		$("#OldMaterialCode").focus();
	}

	plus.nativeUI.closeWaiting();
	//扫描旧条码
	$("#OldMaterialCode").keyup(function(event) {
		event.preventDefault();
		if (event.keyCode == 13) {
			var BarCodeNO = $("#OldMaterialCode").val();
			if (!BarCodeNO) {
				mui.toast("Please scan the Material Barcode。", {
					duration: '1000',
					type: 'div'
				});
				BarCodeNO.focus();
				return false;
			}

			if (BarCodeNO.indexOf("||") !== -1) { // 或者用 includes("||")
				BarCodeNO = BarCodeNO.split("||")[0];
				//console.log("解析到的条码:", firstBarcode); 
			}
			$("#OldMaterialCode").val(BarCodeNO);
			xccFormPostAsync("api/PDA/GetCodeInfo", {
				BarcodeNO: BarCodeNO
			}, function(data) {
				if (data.Code == 200) {
					$("#TotalQty").val(data.Items[0].Qty);
					$("#NewQty").focus();
				}
			}, function(err) {
				$("#txtResult").val(BarCodeNO + "," + error.responseText);
				console.log("没有数据....");
				$("#OldMaterialCode").val('');
				$("#OldMaterialCode").focus();
				plus.nativeUI.closeWaiting(); //关闭转圈圈特效
			})
		}
	});
})
///清空
function CLEAR() {
	// 清空所有输入框
	document.getElementById("OldMaterialCode").value = "";
	document.getElementById("LampNO").value = "";
	document.getElementById("OldQty").value = "";
	document.getElementById("NewMaterialCode").value = "";
	document.getElementById("NewQty").value = "";
	document.getElementById("txtResult").value = "";
	document.getElementById("TotalQty").value = "";
	$("#TradeOrderNO").val('');

	// 将焦点设置回第一个输入框
	document.getElementById("OldMaterialCode").focus();
}
//拆包
function UNPACKING() {
	// 获取输入值
	var oldBarcode = document.getElementById("OldMaterialCode").value.trim();
	var NewQty = document.getElementById("NewQty").value.trim();
	var TotalQty = document.getElementById("TotalQty").value.trim();
	var TradeOrderNO = document.getElementById("TradeOrderNO").value.trim();
	// 判空验证
	if (!oldBarcode) {
		alert("请扫描旧条码！");
		document.getElementById("OldMaterialCode").focus();
		return false;
	}
	if (oldBarcode.indexOf("||") !== -1) { // 或者用 includes("||")
		oldBarcode = oldBarcode.split("||")[0];
		//console.log("解析到的条码:", firstBarcode); 
	}
	$("#OldMaterialCode").val(oldBarcode);
	if (!NewQty) {
		alert("请输入数量！");
		document.getElementById("NewQty").focus();
		return false;
	}

	if (!TotalQty) {
		alert("请输入总数！");
		document.getElementById("TotalQty").focus();
		return false;
	}
	// 旧数量不能大于总数
	if (parseInt(TotalQty) < parseInt(NewQty)) {
		alert("拆包数量不能大于总数数量！");
		document.getElementById("NewQty").focus();
		return false;
	}

	xccFormPostAsync("api/PDA/CodeUnPack", {
		BarcodeNO: oldBarcode,
		Qty: TotalQty,
		NewQty: NewQty,
		TradeOrderNO: TradeOrderNO
	}, function(data) {
		document.activeElement.blur();
		console.log(JSON.stringify(data))
		if (data.Code === 200) {
			$("#NewMaterialCode").val(data.Items[0].BarcodeNO);
			$("#NewQty").val(data.Items[0].Qty);
			$("#OldQty").val(data.Items[0].OldQty);
			$("#txtResult").val(oldBarcode + ",Success!");

		} else {
			$("#txtResult").val(oldBarcode + "," + data.Message);
			mui.toast("Scan failed。" + data.Message, {
				duration: '1000',
				type: 'div'
			});
		}
	}, function(error) {
		$("#txtResult").val(oldBarcode + "," + error.responseText);
		console.log("没有数据....");
	});
}